<template>
	<!-- navbar -->
	<CpNavBar>
		<template v-slot:center>
			发布
		</template>
	</CpNavBar>
	
	<up-notice-bar :text="['云贴使用须知','自由发帖注意言辞']" direction="column"></up-notice-bar>
	<!-- 核心组件 -->
	<PubPost @refSubmit="submitPost" ref="clearPost"></PubPost>
	
	<!-- <up-notify message="Hi uview-plus" :show="loadShow"></up-notify> -->
	<Login ref="LoginRef"></Login>
	<CpTabBar></CpTabBar>
</template>

<script setup>
	import { useTabbarStore } from '@/store/index.js'
	import { ref } from 'vue'
	import Login from '@/components/Login.vue'
	import { useUserStore } from '@/store'
	import PubPost from '@/components/PubPost.vue'
	import { UploadImg } from '@/utils/UploadImg.js'
	
	const userStore = useUserStore()
	const LoginRef = ref(null)
	const clearPost = ref(null)
	// 帖子数据
	const PostData = ref({
		title: '',
		images: [],
		like: 0,
		share: 0,
		comment: 0,
		openid: ''
	})
	const loadShow = ref(true)
	// 发帖
	const submitPost = async (childPost) => {
		PostData.value.title = childPost.title
		PostData.value.images = childPost.images
		
		// 检测内容
		if(!await checkEmpty()) return
		
		uni.showLoading({
			title: '发布中',
			mask: true
		})
		// 上传图片
		const url = PostData.value.images.map(async item => {
			const res = await UploadImg(item)
			return res
		})
		// console.log('url：', url);
		PostData.value.images = await Promise.all(url)
		PostData.value.openid = await userStore.getOpenID()
		// 保存数据库
		await uniCloud.callFunction({
			name: 'add-post',
			data: PostData.value
		})
		// 清空帖子内容
		clearPost.value.clearPost()
		uni.hideLoading()
	}
	
	// 检测内容
	const checkEmpty = async () => {
		// 查询本地是否有openID
		const openID = await userStore.openIDStorage()
		// 检查 textarea 中的内容是否为空
		if (!PostData.value.title) {
		uni.$u.toast('总得写点东西吧')
		return false
		}
	
	  // 检查是否选择了图片
	  if (PostData.value.images.length === 0) {
	    uni.$u.toast('没图怎么看啊')
	    return false
	  }
	  if(!openID){
		LoginRef.value.openPop()
		return false
	  }
	  return true;
	}
	
	const images= [
	        "cloud://env-00jxh5z6ma51/PostImg/2024-08-21/20220801204303_cef03.thumb.400_0_1724230450.jpeg",
	        "cloud://env-00jxh5z6ma51/PostImg/2024-08-21/jellyfish-3840x2160-underwater-25503_1724230450.jpg",
	        "cloud://env-00jxh5z6ma51/PostImg/2024-08-21/macos-rancho-cucamonga-5120x2880-apple-os-x-rancho-cucamonga-abstract-24732_1724230450.jpg",
	        "cloud://env-00jxh5z6ma51/PostImg/2024-08-21/windows_bg_1724230450.png",
	        "cloud://env-00jxh5z6ma51/PostImg/2024-08-21/小程序_15_1724230450.jpg",
	        "cloud://env-00jxh5z6ma51/PostImg/2024-08-21/小程序_30_1724230450.jpg"
	    ]
	const res = images.map(item => {
		return {url: item}
	})
	console.log(res);
	// tabbr
	const tabbarStore = useTabbarStore()
	tabbarStore.setActive(1)
	uni.hideTabBar()
</script>


<style scoped>
	.topic{background-color: #fff;padding: 0 40rpx;margin-bottom: 10rpx;}
	.content{padding: 10rpx 40rpx 0;background-color: #fff;font-size: 28rpx;margin-bottom: 10rpx;}
	.content .picture{padding: 40rpx 0;}
	.tips{padding: 20rpx 40rpx;background-color: #fff;font-size: 28rpx;}
	.submit{padding: 0 40rpx;margin-top: 50rpx;}
</style>
